<template>
	<div>
		<el-form ref="form" label-width="120px" size="small">
			<el-form-item label="公司名称">
				<el-input v-model="newCompany.companyName"></el-input>
			</el-form-item>
			<el-form-item label="公司商标">
				<el-upload
					class="avatar-uploader"
					action="https://img.fsthz.com/upload-shrey-platform.php"
					:show-file-list="false"
					:on-success="handleAvatarSuccess"
					:before-upload="beforeAvatarUpload">
					<img v-if="imageUrl" :src="imageUrl" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
			<el-form-item label="公司地址">
				<el-input v-model="newCompany.address"></el-input>
			</el-form-item>
			<el-form-item label="联系人">
				<el-input v-model="newCompany.contact"></el-input>
			</el-form-item>
			<el-form-item label="电话">
				<el-input v-model="newCompany.phone"></el-input>
			</el-form-item>

			<el-form-item label="可用">
				<el-switch on-text="" off-text="" v-model="newCompany.isEnable"></el-switch>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="cancel">取消</el-button>
			<el-button type="primary" @click="submit">提交</el-button>
		</span>
	</div>

</template>

<script>
	export default {
		data(){
			return {
				defaultImg:'http://img.fsthz.com/uploads/shrey-platform/no-company-logo.jpg',
				imageUrl:''
			}
		},
		props:['newCompany'],
		computed:{
			user:function(){
				return this.$store.getters.user;
			}
		},
		methods:{
			handleAvatarSuccess(res, file){
				if(res.url){
					this.imageUrl = URL.createObjectURL(file.raw);
					this.newCompany.logo = res.url;
				}
			},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'image/jpeg';
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isJPG) {
					this.$message.error('Avatar picture must be JPG format!');
				}
				if (!isLt2M) {
					this.$message.error('Avatar picture size can not exceed 2MB!');
				}
				return isJPG && isLt2M;
			},
			validateForm(form){
				let result = undefined;
				if(!form.companyName) result = '公司名称';
				if(!form.address) result = '地址';
				if(!form.phone) result = '电话';
				if(!form.contact) result = '联系人';
				if(result){
					this.$message({
						message:result + '不能为空',
						type: 'error'
					});
					return false;
				}else{
					return true;
				}
			},
			submit(){
				if(!this.newCompany.logo) this.newCompany.logo = this.defaultImg;
				if(this.validateForm(this.newCompany)){
					this.$store.dispatch({
						type:'fetch',
						name:'createCompany',
						data:{
							...this.newCompany,
						},
					}).then(res=>{
						if(res.errCode){
							this.$message({
								message:res.errMsg,
								type: 'error'
							});
						}else{
							this.$message({
								message:'添加成功',
								type: 'success'
							});
							this.$emit('cancel');
						}
					}).catch(error=>{
						this.$message({
							message:'连不上服务器',
							type: 'error'
						});
					})
				}
			},
			cancel(){
				this.$emit('cancel');
			},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'image/jpeg';
				const isLt2M = file.size / 1024 / 1024 < 2;

				if (!isJPG) {
					this.$message.error('Avatar picture must be JPG format!');
				}
				if (!isLt2M) {
					this.$message.error('Avatar picture size can not exceed 2MB!');
				}
				return isJPG && isLt2M;
			}
		}
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}
	.avatar-uploader .el-upload:hover {
		border-color: #20a0ff;
	}
	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}
	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
